//
//  DBManager.m
//  SouDog
//
//  Created by  on 15/8/18.
//  Copyright (c) 2015年 @SHui. All rights reserved.
//

#import "DBManager.h"
#import "FMDB.h"
#import "ImageDataManager.h"


static DBManager * g_Manager =nil;

@implementation DBManager
{

    FMDatabase * _dataBase;
}
//单例 线程管理者
+ (id)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (g_Manager == nil) {
            g_Manager = [[DBManager alloc] init];
        }
    });
    return g_Manager;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        //在沙盒下创建sougou.db" 文件
        NSString *dbPath = [NSString stringWithFormat:@"%@/Documents/ShangPiBao.db",NSHomeDirectory()];
        _dataBase = [[FMDatabase alloc] initWithPath:dbPath];
        if ([_dataBase open]) {
            
            NSString *createStr = @"create table if not exists collection(appId,name,time)";
            
            BOOL isSuc = [_dataBase executeUpdate:createStr];
            
            if (isSuc) {
                NSLog(@"打开或创建数据库成功！");
            }else {
                NSLog(@"打开或创建数据库失败！");
            }
        }
        
    }
    return self;
}
-(BOOL)isDataExistByAppId:(NSString *)appId{

    FMResultSet * set =[_dataBase executeQuery:@"select * from collection where appId=?",appId];
    if ([set next]) {
        return YES;
    }else{
        return NO;
    }
    

}
//添加数据
-(BOOL)addDataFromModel:(DBModel *)model{
    BOOL isExist =[self isDataExistByAppId:model.appId];
    if (isExist) {
        NSLog(@"该数据已经存在");
        return NO;
    }else{
        //将图片数据存储到本地沙盒
//        BOOL saveImageSuc =[[ImageDataManager sharedImageDataManager] addImageDataWithDBModel:model];
//        
//        if (saveImageSuc) {
//            //将name 加入到数据库
            BOOL isSuc =  [_dataBase executeUpdate:@"insert into collection values (?,?,?)",model.appId,model.name,model.time];
            if (isSuc) {
                 NSLog(@"加入数据库成功");
            }else{
                NSLog(@"DBerror = %@",_dataBase.lastErrorMessage);
            }
           
            return YES;
//        }else{
//            return NO;
//        }
    
    }

}

-(BOOL)deleteDataByModel:(DBModel *)model{
//判断数据是否存在
    BOOL isExist =[self isDataExistByAppId:model.appId];
    if (isExist) {
        //删除图片数据
//        BOOL isDeImageSuc =[[ImageDataManager sharedImageDataManager]deleteImageDataWithDBmodel:model];
//        if (isDeImageSuc) {
            [_dataBase executeUpdate:@"delete from collection where appId =?",model.appId];
            return YES;
//        }else{
//        
//            NSLog(@"删除失败");
//            return NO;
//        
//        }
    }else{
    
        NSLog(@"该数据库中不存在该数据");
        return NO;
    
    }

}

-(NSMutableArray *)fetchAllData{
    FMResultSet * set =[_dataBase executeQuery:@"select * from collection"];
    
    NSMutableArray * dataArray =[NSMutableArray array];
    while ([set next]) {
        DBModel * model =[[DBModel alloc]init];
        
        model.appId =[set stringForColumn:@"appId"];
        model.name =[set stringForColumn:@"name"];
        model.time =[set stringForColumn:@"time"];
      
        [dataArray addObject:model];
    }
    return dataArray;
}
@end
